<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="门店" prop="storeId">
                <el-select
                  v-model="queryParams.storeId"
                  placeholder="请选择"
                  class="w-200"
                  filterable
                >
                  <el-option
                    v-for="item in this.$store.state.user.stores"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="任务状态" prop="taskStatus">
                <el-select
                  v-model="queryParams.taskStatus"
                  placeholder="请选择"
                  class="w-200"
                >
                  <el-option
                    v-for="item in taskStatusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="任务月份" prop="date">
                <el-date-picker
                  v-model="queryParams.taskMoon"
                  type="month"
                  placeholder="选择月"
                  value-format="yyyy-MM"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>

    <!-- 业绩任务 -->
    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">业绩任务</div>
        <div class="op_right">
          <button class="primary_button" @click="handleAddTask">
            新增业绩
          </button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="taskData" stripe style="width: 100%">
          <el-table-column prop="taskMoon" label="任务月份"> </el-table-column>
          <el-table-column prop="storeName" label="门店"></el-table-column>
          <el-table-column prop="taskTurnover" label="本月任务">
          </el-table-column>

          <el-table-column
            prop="taskCardMoney"
            label="充值金额"
          ></el-table-column>
          <el-table-column prop="taskCustomerCount" label="客量任务">
          </el-table-column>
          <el-table-column
            prop="taskClockAddCount"
            label="加钟数"
          ></el-table-column>
          <el-table-column
            prop="taskClockPointCount"
            label="点客数"
          ></el-table-column>

          <el-table-column prop="taskCustomerVisitorCount" label="约客">
          </el-table-column>

          <el-table-column prop="taskStatusStr" label="任务状态">
          </el-table-column>

          <el-table-column label="操作" width="150px">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="EditData(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                @click="CancleData(scope.row)"
                v-if="
                  scope.row.taskStatus === 'W' || scope.row.taskStatus === 'F'
                "
                >取消</el-button
              >

              <el-button
                size="mini"
                type="text"
                @click="CancleQuash(scope.row)"
                v-if="scope.row.taskStatus === 'C'"
                >取消撤销</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 门店业绩任务 创建任务 -->
      <el-dialog
        :title="title"
        :visible.sync="CancleQuashVisible"
        width="900px"
        :before-close="CancleQuashHandleClose"
      >
        <span>
          <el-form
            :model="TasksForm"
            :rules="CancleQuashRules"
            ref="TasksForm"
            label-width="100px"
            class="no-dialog-padding"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="任务月份" prop="taskMoon">
                  <el-date-picker
                    v-model="TasksForm.taskMoon"
                    type="month"
                    placeholder="选择月"
                    class="w-100"
                    value-format="yyyy-MM"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="门店" prop="storeId">
                  <el-select
                    v-model="TasksForm.storeId"
                    placeholder="请选择"
                    class="w-100"
                    filterable
                  >
                    <el-option
                      v-for="item in this.$store.state.user.stores"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="门店任务" prop="taskTurnover">
                  <el-input-number
                    v-model="TasksForm.taskTurnover"
                    placeholder="请输入"
                    :precision="0"
                    :step="1"
                    :min="0"
                    :controls="false"
                    class="w-100 text-left"
                  ></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="待客任务" prop="taskCustomerCount">
                  <el-input-number
                    v-model="TasksForm.taskCustomerCount"
                    placeholder="请输入"
                    :precision="0"
                    :step="1"
                    :min="0"
                    :controls="false"
                    class="w-100 text-left"
                  ></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="加钟任务" prop="taskClockAddCount">
                  <el-input-number
                    v-model="TasksForm.taskClockAddCount"
                    placeholder="请输入"
                    :precision="0"
                    :step="1"
                    :min="0"
                    :controls="false"
                    class="w-100 text-left"
                  ></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="点客任务" prop="taskClockPointCount">
                  <el-input-number
                    v-model="TasksForm.taskClockPointCount"
                    placeholder="请输入"
                    :precision="0"
                    :step="1"
                    :min="0"
                    :controls="false"
                    class="w-100 text-left"
                  ></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="充卡任务" prop="taskCardMoney">
                  <el-input-number
                    v-model="TasksForm.taskCardMoney"
                    placeholder="请输入"
                    :precision="0"
                    :step="1"
                    :min="0"
                    :controls="false"
                    class="w-100 text-left"
                  ></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="约客任务" prop="taskCustomerVisitorCount">
                  <el-input-number
                    v-model="TasksForm.taskCustomerVisitorCount"
                    placeholder="请输入"
                    :precision="0"
                    :step="1"
                    :min="0"
                    :controls="false"
                    class="w-100 text-left"
                  ></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>

          <!-- 门店业绩任务历史 -->
          <div v-if="!!this.TasksForm.taskId">
            <el-table :data="HistoryTaskData" style="width: 100%">
              <el-table-column prop="taskHisMoon" label="任务月份">
              </el-table-column>
              <el-table-column prop="storeName" label="门店名称" width="130">
              </el-table-column>
              <el-table-column prop="taskHisTurnover" label="本月任务">
              </el-table-column>
              <el-table-column prop="taskHisCardMoney" label="充值金额">
              </el-table-column>
              <el-table-column prop="taskHisCustomerCount" label="客户任务">
              </el-table-column>
              <el-table-column prop="taskHisClockAddCount" label="加钟任务">
              </el-table-column>
              <el-table-column prop="taskHisClockPointCount" label="点客任务">
              </el-table-column>

              <el-table-column
                prop="taskHisCustomerVisitorCount"
                label="约客任务"
              >
              </el-table-column>
              <el-table-column
                prop="userName"
                label="修改人"
              >
              </el-table-column>
              <el-table-column prop="createTime" label="修改时间" width="150px">
              </el-table-column>
            </el-table>
            <pagination
              :total="HistoryTotal"
              :page.sync="HistoryParams.pageNo"
              :limit.sync="HistoryParams.pageSize"
              @pagination="getHistoryTaskList"
            />
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button
            type="primary"
            @click="SureConfirms"
            v-if="!!TasksForm.taskId"
            >确定业绩</el-button
          >
          <el-button @click="CancleQuashHandleClose">取 消</el-button>
          <el-button type="primary" @click="CancleQuashSubmit" :loading="RaskLoading">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
  
  <script>
import {
  getTaskHistoryPage,
  CreateTasks,
  UpdateTasks,
  getTaskPage,
  getCancelTask,
  getStoreTaskPage,
  SureConfirm,
} from "@/api/business/task";

export default {
  name: "EryuoaStoreTaskList",

  data() {
    return {
      RaskLoading:false,
      CancleQuashVisible: false,
      TasksForm: {
        taskId: "",
        storeId: "",
        taskCardMoney: "",
        taskClockAddCount: "",
        taskClockPointCount: "",
        taskCustomerCount: "",
        taskCustomerVisitorCount: "",
        taskMoon: "",
        taskTurnover: "",
      },

      HistoryParams: {
        taskId: "",
        pageNo: 1,
        pageSize: 10,
      },

      HistoryTaskData: [],
      CancleQuashRules: {
        storeId: [{ required: true, message: "请选择门店", trigger: "change" }],
        taskMoon: [
          { required: true, message: "请选择任务月份", trigger: "change" },
        ],
        taskTurnover: [
          { required: true, message: "请输入门店任务", trigger: "blur" },
        ],
        taskCustomerCount: [
          { required: true, message: "请输入待客任务", trigger: "change" },
        ],
        taskClockAddCount: [
          { required: true, message: "请输入加钟任务", trigger: "change" },
        ],
        taskClockPointCount: [
          { required: true, message: "请输入点客任务", trigger: "change" },
        ],
        taskCardMoney: [
          { required: true, message: "请输入充卡任务", trigger: "change" },
        ],
        taskCustomerVisitorCount: [
          { required: true, message: "请输入约客任务", trigger: "change" },
        ],
      },

      loading: false,

      taskData: [], // 项目列表数据
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        storeId: "",
        taskStatus: "",
        taskMoon: "",
      },

      taskStatusOptions: [
        {
          value: "F",
          label: "已确定",
        },
        {
          value: "W",
          label: "待确定",
        },
        {
          value: "C",
          label: "取消",
        },
      ],
      total: 0,
      HistoryTotal: 0,
      title: "",
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    SureConfirms() {
      this.$confirm("要确认业绩吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          SureConfirm({
            
            taskId: this.TasksForm.taskId,
          })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getList();
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.CancleQuashHandleClose();
              }
            })
            .catch((err) => {});
        })
        .catch(() => {});
    },

    // 编辑
    EditData(row) {
      this.CancleQuashVisible = true;
      this.title = "门店任务制定";
      this.HistoryParams.taskId = row.taskId;
      this.HistoryParams.pageNo = 1;
      this.getStoreTaskDetail(row.taskId);
      this.getHistoryTaskList();
    },

    // 取消
    CancleData(row) {
      this.$confirm("确认要取消吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          getCancelTask({
            taskId: row.taskId,
          })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getList();
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
              }
            })
            .catch((err) => {});
        })
        .catch(() => {});
    },

    // 撤销取消
    CancleQuash(row) {
      this.$confirm("确认要取消撤销吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          getCancelTask({
            taskId: row.taskId,
          })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getList();
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
              }
            })
            .catch((err) => {});
        })
        .catch(() => {});
    },

    CancleQuashHandleClose() {
      this.CancleQuashVisible = false;
      this.TasksForm.taskId = "";
      this.resetFormData("TasksForm");
    },

    CancleQuashSubmit() {
      this.RaskLoading = true;
      this.$refs["TasksForm"].validate((valid) => {
        if (valid) {
          if (!this.TasksForm.taskId) {
            this.CreateTask();
          } else {
            this.UpdateTask();
          }
        } else {
          return false;
        }
      });
    },

    // 详情
    getStoreTaskDetail(taskId) {
      getStoreTaskPage({ taskId: taskId })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            this.TasksForm.storeId = data.storeId;
            this.TasksForm.taskId = data.taskId;
            this.TasksForm.storeName = data.storeName;
            this.TasksForm.taskCardMoney = data.taskCardMoney;
            this.TasksForm.taskClockAddCount = data.taskClockAddCount;
            this.TasksForm.taskClockPointCount = data.taskClockPointCount;
            this.TasksForm.taskCustomerCount = data.taskCustomerCount;
            this.TasksForm.taskCustomerVisitorCount =
              data.taskCustomerVisitorCount;
            this.TasksForm.taskMoon = data.taskMoon;
            this.TasksForm.taskTurnover = data.taskTurnover;
          }
        })
        .catch((err) => {});
    },

    // 新增
    CreateTask() {
      this.RaskLoading = true;
      CreateTasks(this.TasksForm)
        .then((res) => {
          this.RaskLoading = false;
          const { code, data } = res;
          if (code === 0) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.getList();
            this.CancleQuashHandleClose();
          }
        })
        .catch((err) => {
          this.RaskLoading = false;
        });
    },

    // 编辑
    UpdateTask() {
      this.RaskLoading = true;
      UpdateTasks(this.TasksForm)
        .then((res) => {
          this.RaskLoading = false;
          const { code, data } = res;
          if (code === 0) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.getList();
            this.CancleQuashHandleClose();
          }
        })
        .catch((err) => {
          this.RaskLoading = false;
        });
    },

    // 业绩任务列表
    getList() {
      this.loading = true;
      getTaskPage(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.taskData = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 门店业绩任务历史
    getHistoryTaskList() {
      this.loading = true;

      getTaskHistoryPage(this.HistoryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.HistoryTaskData = data.list;
            this.HistoryTotal = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
    },

    // 表单重置
    resetFormData(formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    handleAddTask() {
      this.CancleQuashVisible = true;
      this.title = "门店任务制定";
    },
  },
};
</script>
  
  <style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}

.separate_left {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-right: 12px;
}
.separate_right {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-left: 12px;
}

.check {
  font-size: 12px;
  color: #0052d9;
}

.list_Stores {
  height: 200px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.9);
  line-height: 22px;
  .list_Stores_item {
    height: 46px;
    line-height: 46px;
    background: #ffffff;
    padding-left: 47px;
    border-bottom: 1px solid #e7e7e7;
  }
}
</style>
  